# -*- encoding: utf-8 -*-
"""
@Author  :   BillFun
@License :   (C) Copyright 2013-2017, JieInfo
@Software:   PyCharm
@File    :   logger.py
@Time    :   2018/3/29 23:25
@Desc    :
"""

__all__ = ['set_logger', 'debug', 'info', 'warning', 'error',
           'critical', 'exception']

import logging
import logging.handlers
import os
import sys

# Global logger
g_logger = None


def init_logger():
    global g_logger

    if g_logger is None:
        g_logger = logging.getLogger()
    else:
        logging.shutdown()
        g_logger.handlers = []

    g_logger.setLevel(logging.DEBUG)


def set_logger():
    init_logger()
    # add_streamhandler(s_level, fmt)
    # add_filehandler(f_level, fmt, filename, mode, backup_count, limit, when)

    # Import the common log functions for convenient
    import_log_funcs()


def import_log_funcs():
    """Import the common log functions from the global logger to the module."""
    global g_logger

    curr_mod = sys.modules[__name__]
    log_funcs = ['debug', 'info', 'warning', 'error', 'critical',
                 'exception']

    for func_name in log_funcs:
        func = getattr(g_logger, func_name)
        setattr(curr_mod, func_name, func)


if __name__ == '__main__':
    pass
